<template>
  <div 
    :class="[
      'bx-loading',
      {
        'bx-loading--overlay': overlay,
        [`bx-loading--${size}`]: size
      }
    ]"
    v-show="loading"
  >
    <div v-if="overlay" class="bx-loading__mask bx-glass"></div>
    <div class="bx-loading__content">
      <div class="bx-loading__spinner">
        <div v-if="indicator === 'spinner'" class="bx-loading__spinner-default">
          <div class="bx-loading__dot" v-for="i in 12" :key="i"></div>
        </div>
        <div v-else-if="indicator === 'dots'" class="bx-loading__dots">
          <div class="bx-loading__dot" v-for="i in 3" :key="i"></div>
        </div>
        <div v-else-if="indicator === 'pulse'" class="bx-loading__pulse">
          <div class="bx-loading__pulse-ring" v-for="i in 3" :key="i"></div>
        </div>
        <component v-else-if="typeof indicator === 'object'" :is="indicator" />
      </div>
      <div v-if="text || $slots.default" class="bx-loading__text">
        <slot>{{ text }}</slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BxLoading',
  props: {
    loading: {
      type: Boolean,
      default: true
    },
    text: {
      type: String,
      default: ''
    },
    indicator: {
      type: [String, Object],
      default: 'spinner',
      validator: (value) => {
        if (typeof value === 'string') {
          return ['spinner', 'dots', 'pulse'].includes(value)
        }
        return true
      }
    },
    size: {
      type: String,
      default: 'medium',
      validator: (value) => ['small', 'medium', 'large'].includes(value)
    },
    overlay: {
      type: Boolean,
      default: false
    }
  }
}
</script>

<style scoped>
.bx-loading {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--bx-font-family);
}

.bx-loading--overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2000;
  display: flex;
}

.bx-loading__mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(8px);
}

.bx-loading__content {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--bx-spacing-md);
  z-index: 1;
}

.bx-loading__spinner {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 默认旋转器 */
.bx-loading__spinner-default {
  position: relative;
  width: 32px;
  height: 32px;
}

.bx-loading--small .bx-loading__spinner-default {
  width: 24px;
  height: 24px;
}

.bx-loading--large .bx-loading__spinner-default {
  width: 40px;
  height: 40px;
}

.bx-loading__spinner-default .bx-loading__dot {
  position: absolute;
  width: 3px;
  height: 8px;
  background: var(--bx-text-primary);
  border-radius: 2px;
  opacity: 0.3;
  animation: bx-loading-spinner 1.2s linear infinite;
}

.bx-loading--small .bx-loading__spinner-default .bx-loading__dot {
  width: 2px;
  height: 6px;
}

.bx-loading--large .bx-loading__spinner-default .bx-loading__dot {
  width: 4px;
  height: 10px;
}

.bx-loading__spinner-default .bx-loading__dot:nth-child(1) { transform: rotate(0deg) translate(0, -12px); animation-delay: 0s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(2) { transform: rotate(30deg) translate(0, -12px); animation-delay: 0.1s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(3) { transform: rotate(60deg) translate(0, -12px); animation-delay: 0.2s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(4) { transform: rotate(90deg) translate(0, -12px); animation-delay: 0.3s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(5) { transform: rotate(120deg) translate(0, -12px); animation-delay: 0.4s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(6) { transform: rotate(150deg) translate(0, -12px); animation-delay: 0.5s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(7) { transform: rotate(180deg) translate(0, -12px); animation-delay: 0.6s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(8) { transform: rotate(210deg) translate(0, -12px); animation-delay: 0.7s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(9) { transform: rotate(240deg) translate(0, -12px); animation-delay: 0.8s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(10) { transform: rotate(270deg) translate(0, -12px); animation-delay: 0.9s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(11) { transform: rotate(300deg) translate(0, -12px); animation-delay: 1.0s; }
.bx-loading__spinner-default .bx-loading__dot:nth-child(12) { transform: rotate(330deg) translate(0, -12px); animation-delay: 1.1s; }

@keyframes bx-loading-spinner {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

/* 点状加载器 */
.bx-loading__dots {
  display: flex;
  gap: 4px;
}

.bx-loading__dots .bx-loading__dot {
  width: 8px;
  height: 8px;
  background: var(--bx-text-primary);
  border-radius: 50%;
  animation: bx-loading-dots 1.4s ease-in-out infinite both;
}

.bx-loading--small .bx-loading__dots .bx-loading__dot {
  width: 6px;
  height: 6px;
}

.bx-loading--large .bx-loading__dots .bx-loading__dot {
  width: 10px;
  height: 10px;
}

.bx-loading__dots .bx-loading__dot:nth-child(1) { animation-delay: -0.32s; }
.bx-loading__dots .bx-loading__dot:nth-child(2) { animation-delay: -0.16s; }
.bx-loading__dots .bx-loading__dot:nth-child(3) { animation-delay: 0s; }

@keyframes bx-loading-dots {
  0%, 80%, 100% {
    transform: scale(0);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 脉冲加载器 */
.bx-loading__pulse {
  position: relative;
  width: 32px;
  height: 32px;
}

.bx-loading--small .bx-loading__pulse {
  width: 24px;
  height: 24px;
}

.bx-loading--large .bx-loading__pulse {
  width: 40px;
  height: 40px;
}

.bx-loading__pulse-ring {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 2px solid var(--bx-text-primary);
  border-radius: 50%;
  opacity: 0;
  animation: bx-loading-pulse 2s ease-in-out infinite;
}

.bx-loading__pulse-ring:nth-child(1) { animation-delay: 0s; }
.bx-loading__pulse-ring:nth-child(2) { animation-delay: 0.6s; }
.bx-loading__pulse-ring:nth-child(3) { animation-delay: 1.2s; }

@keyframes bx-loading-pulse {
  0% {
    transform: scale(0);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 0;
  }
}

.bx-loading__text {
  color: var(--bx-text-primary);
  font-size: 14px;
  line-height: 1.5;
  text-align: center;
}

.bx-loading--small .bx-loading__text {
  font-size: 12px;
}

.bx-loading--large .bx-loading__text {
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-loading__content {
    gap: var(--bx-spacing-sm);
  }
  
  .bx-loading__text {
    font-size: 13px;
  }
}
</style>

